package servlet;

import com.octo.captcha.service.CaptchaServiceException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.CaptchaServiceSingleton;
import util.Mail;
import util.Utilities;

/**
 *
 * @author HOME
 * @version
 */
public class RegisterServlet extends HttpServlet {

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("userID");
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        String interest = "silly interest";
        String country = "China";
        String zipcode = "1234";
        String address = request.getParameter("address");
        String password = request.getParameter("password9");
        String repassword = request.getParameter("repassword");
        String email = request.getParameter("email");
        String captchaCode = request.getParameter("captchacode");
        boolean isCorrectCaptcha = false;
        boolean ok = false;

        //move into the if selection later
        HttpSession session = request.getSession(true);
        session.setAttribute("userID", id);
        session.setAttribute("firstname", firstname);
        session.setAttribute("lastname", lastname);
        session.setAttribute("age", age);
        session.setAttribute("gender", gender);
        session.setAttribute("address", address);
        session.setAttribute("email", email);

        try {
            isCorrectCaptcha = CaptchaServiceSingleton.getInstance().validateResponseForID(session.getId(), captchaCode);
        } catch (CaptchaServiceException e) {
            //should not happen, may be thrown if the id is not valid
        }

        //todo: check for the already exist user id
        //todo: check for the already exist email.
        //todo: check for account activation, resend comfirmation letter if necessary
        
        if (Utilities.isValid(password) && !password.equals(repassword)) {
            response.sendRedirect("Registration.jsp?msg=4");
            return;
        } else if (!isCorrectCaptcha) {
            response.sendRedirect("Registration.jsp?msg=1");
            return;
        } else if (beans.UserBean.isIDExist(id)) {
            response.sendRedirect("Registration.jsp?msg=2");
            return;
        } else {
            boolean creationSucceed = beans.MemberBean.memberRegister(id, firstname, lastname, age, gender, interest, country,
                    zipcode, address, password, email);
            if (creationSucceed) {
                //TODO:create the MD5 hash and take the first 10 characters as activation code
                // Using the MD5Hash
                
                
                ok = Mail.sendMail("二手易", email, "Confirmation mail for 2 Hand Easy");
                response.sendRedirect("Registered.jsp");
                return;
            } else {
                response.sendRedirect("Registration.jsp?msg=3");
                return;
            }

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
// </editor-fold>
}
